$(function () {
    var $title = $("#title")
    var $demoBox = $(".demo-box")

    var $donelist = $("#donelist")
    // 实现添加子项功能
    $title.on("keyup", function (evt) {
        var e = evt || event
        if (e.keyCode === 13) {
            createlist()
            $title.val("")
            count()
        }
    })
    function edit(num) {
        console.log(num);
    }
    function createlist() {
        listAdd($title)
        $demoBox.append(`
        <li>
            <input type="checkbox" />
            <p>${$title.val()}</p>
            <a href="javascript:remove(1)">-</a>
        </li>
        `)
    }
    var $section = $("section")
    // 实现根据复选框的状态改变列的位置
    $section.on("change", "input[type=checkbox]", function () {
        var $d = $(this).parent().children("p")
        var n=$donelist.children().length
        console.log(n);
        var list = getLocalStorage()
        if (this.checked) {
            $donelist.append($(this).parent())
            for (var attr in list) {
                if ($d.text() === list[attr]) {
                    if (/domeBox/ig.test(attr)) {
                        list["donelist" + list[attr]] = list[attr]
                        setLocalStorage(list)
                        listRemoveData(list[attr]);
                    }
                }
            }
        }
        else {
            $demoBox.append($(this).parent())
            list = getLocalStorage()
            for (var attr in list) {
                if ($d.text() === list[attr]) {
                    if (/donelist/ig.test(attr)) {
                        list["domeBox" + list[attr]] = list[attr]
                        console.log(list);
                        setLocalStorage(list)
                        listRemoveData(list[attr]);
                    }
                }
            }
        }
        count()
    })
    // 实现删除
    $section.on("click", "li a", function () {
        $(this).parent().remove()
        count()
        listRemove($(this).siblings("p"))
    })
    // 计数
    var $donecount = $("#donecount")
    var $todocount = $("#todocount")
    function count() {
        $todocount.text($demoBox.children().length)
        $donecount.text($donelist.children().length)
    }
    // $.ajax({
    //     type: "GET",
    //     url: "https://api.i-lynn.cn/getIpInfo",
    //     jsonp: "cb",
    //     dataType: "jsonp",
    //     success: function (res) {
    //         alert(res);
    //     }
    // })
    function listRemove($dom) {
        var list = getLocalStorage()
        delete list["domeBox" + $dom.text()]
        setLocalStorage(list)
    }
    function listRemoveData(data) {
        var list = getLocalStorage()
        delete list["domeBox" + data]
        console.log(list);
        setLocalStorage(list)
    }
    function listAdd($dom) {
        var list = getLocalStorage()
        list["domeBox" + $dom.val()] = $dom.val()
        setLocalStorage(list)
    }
    function listUnload() {

    }
    function getLocalStorage() {
        var todolist = localStorage.getItem("toDoList")
        if (todolist) {
            return JSON.parse(todolist)
        }
        else {
            return {}
        }
    }
    function setLocalStorage(data) {
        localStorage.setItem("toDoList", JSON.stringify(data))
    }
    function render() {
        var list = getLocalStorage()
        for (var attr in list) {
            // console.log(attr);
            if (/domeBox/ig.test(attr)) {
                var html = `
                <li>
                    <input type="checkbox" />
                    <p>${list[attr]}</p>
                    <a href="javascript:remove(1)">-</a>
                </li>
                `
                $demoBox.append(html)
            }
        }
        count()
    }
    render()
})